package com.yanqu.road.server.cmd.activity.musicroomrank.cross;

import com.yanqu.road.dao.impl.player.UserMusicRoomRankBeautyDaoImpl;
import com.yanqu.road.dao.impl.player.UserMusicRoomRankDaoImpl;
import com.yanqu.road.entity.activity.musicroomrank.MusicRoomRankConfigRankData;
import com.yanqu.road.entity.activity.musicroomrank.UserMusicRoomRankBeautyData;
import com.yanqu.road.entity.activity.musicroomrank.UserMusicRoomRankData;
import com.yanqu.road.entity.log.LogMusicRoomRankShowResult;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.MusicRoomRankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.musicroom.MusicRoomRankActivityModule;
import com.yanqu.road.server.manger.activity.musicroomrank.MusicRoomRankActivityMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_MUSIC_ROOM_RANK_SHOW_RESULT_CROSS_BACK, desc = "妙音坊冲榜 评选结果返回")
public class MusicRoomRankShowResultCrossBackCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MusicRoomRankProto.CrossMusicRoomRankShowResultResultMsg backMsg = MusicRoomRankProto.CrossMusicRoomRankShowResultResultMsg.parseFrom(packet.getBytes());

        Map<Long, List<MusicRoomRankProto.MusicRoomRankShowTemp>> userResultMap = new HashMap<>();

        for (MusicRoomRankProto.MusicRoomRankShowTemp rankShowTemp : backMsg.getRankDataList()) {
            long userId = rankShowTemp.getUserId();
            long serverId = rankShowTemp.getUserBaseInfo().getServerId();
            if (serverId != GameServer.getInstance().getServerId()) {
                continue;
            }
            List<MusicRoomRankProto.MusicRoomRankShowTemp> rankShowTempList = userResultMap.get(userId);
            if (rankShowTempList == null) {
                rankShowTempList = new ArrayList<>();
                userResultMap.put(userId, rankShowTempList);
            }
            rankShowTempList.add(rankShowTemp);
        }

        MusicRoomRankActivityMgr.addTask(backMsg.getRound(), backMsg.getTheme(), userResultMap);

        GamePlayerMgr.sendPacket(0,
                YanQuMessageUtils.buildMessage(CrossProtocol.C_MUSIC_ROOM_RANK_SHOW_RESULT_CONFIRM_BACK,
                        MusicRoomRankProto.CrossMusicRoomRankHistoryShowDetailReqMsg.newBuilder().setActivityId(backMsg.getActivityId()).setRound(backMsg.getRound())));
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
